#include "Settings.h"

Settings::Settings(){
    this->settings = new QSettings("Polytech_Lille", "Multimedia_Learner");
}

Settings::~Settings(){
    delete this->settings;
}

bool Settings::firstLaunchTime(){
    bool firstTime = !this->settings->contains("firstTime");
    this->settings->setValue("firstTime", false);
    return firstTime;
}

void Settings::generateDefaultOptions(){
    this->setCrashed(false);
    //01 : interface
    this->setContinuePlaying(false);
    QString locale = QLocale::system().name().section('_', 0, 0);
    QStringList allLangages = this->getAvailableLangages();
    if(!allLangages.contains(locale)){
        locale = "en";
    }
    this->setLangage(locale);
    this->setIdLangage(allLangages.indexOf(locale));
    this->setIdExtractionMethod(0);
    // 01.1 method 01
    this->setAutomaticMaxNSecBefore(5);
    this->setAutomaticMaxNSecAfter(5);
    this->setAutomaticSensibility(60);
    this->setAutomaticMedianDataSize(12);
    // 01.1 method 02
    this->setNormalNSecBefore(2);
    this->setNormalNSecAfter(2);
    //02 : subtitle
    //TODO
    //03 : video
    //04 : audio
    this->setVolume(100);
}

QStringList Settings::getAvailableLangages(){
    QStringList stringList;
    QDirIterator dirIterator(":/langages");
    stringList.append("fr");
    while(dirIterator.hasNext()){
        QString langageName(dirIterator.next());
        if(langageName.endsWith("qm")){
            langageName = langageName.right(5).left(2); //TODO set a more beautiful langage name
            stringList.append(langageName);
        }
    }
    return stringList;
}

//00 : other
bool Settings::hasCrashed(){
    return this->settings->value("crashed").toBool();
}

void Settings::setCrashed(bool crashed){
    this->settings->setValue("crashed", crashed);
}

//01 : interface
void Settings::setContinuePlaying(bool continuePlaying){
    this->settings->setValue("continuePlaying", continuePlaying);
}

bool Settings::isContinuePlaying(){
    return this->settings->value("continuePlaying").toBool();
}

void Settings::setLangage(QString langage){
    this->settings->setValue("langage", langage);
}

QString Settings::getLangage(){
    return this->settings->value("langage").toString();
}

void Settings::setIdLangage(int idLangage){
    this->settings->setValue("idLangage", idLangage);
}

int Settings::getIdLangage(){
    return this->settings->value("idLangage").toInt();
}

void Settings::setIdExtractionMethod(int id){
    this->settings->setValue("idExtractionMethod", id);
}

int Settings::getIdExtractionMethod(){
    return this->settings->value("idExtractionMethod").toInt();
}
// 01.1 method 01 automatic
void Settings::setAutomaticMaxNSecBefore(int nSecBefore){
    this->settings->setValue("automaticMaxNSecBefore", nSecBefore);
}

int Settings::getAutomaticMaxNSecBefore(){
    return this->settings->value("automaticMaxNSecBefore").toInt();
}

void Settings::setAutomaticMaxNSecAfter(int nSecAfter){
    this->settings->setValue("automaticMaxNSecAfter", nSecAfter);
}

int Settings::getAutomaticMaxNSecAfter(){
    return this->settings->value("automaticMaxNSecAfter").toInt();
}

void Settings::setAutomaticSensibility(int sensibility){
    this->settings->setValue("automaticSensibility", sensibility);
}

int Settings::getAutomaticSensibility(){
    return this->settings->value("automaticSensibility").toInt();
}

void Settings::setAutomaticMedianDataSize(int medianDataSize){
    this->settings->setValue("automaticMedianDataSize", medianDataSize);
}

int Settings::getAutomaticMedianDataSize(){
    return this->settings->value("automaticMedianDataSize").toInt();
}

// 01.1 method 02 normal
void Settings::setNormalNSecBefore(int nSecBefore){
    this->settings->setValue("normalNSecBefore", nSecBefore);
}

int Settings::getNormalNSecBefore(){
    return this->settings->value("normalNSecBefore").toInt();
}

void Settings::setNormalNSecAfter(int nSecAfter){
    this->settings->setValue("normalNSecAfter", nSecAfter);
}

int Settings::getNormalNSecAfter(){
    return this->settings->value("normalNSecAfter").toInt();
}


//02 : subtitle
void Settings::setSubtitlePropertie01(QFont font, QColor color, int y){
    this->settings->setValue("sub01_font_family", font.family());
    this->settings->setValue("sub01_font_size", font.pointSize());
    this->settings->setValue("sub01_font_weight", font.weight());
    this->settings->setValue("sub01_font_italic", font.italic());
    this->settings->setValue("sub01_color_red", color.red());
    this->settings->setValue("sub01_color_green", color.green());
    this->settings->setValue("sub01_color_blue", color.blue());
    this->settings->setValue("sub01_y", y);
}

QFont Settings::getFontSubtitlePropertie01(){
    QString family(this->settings->value("sub01_font_family").toString());
    int size = this->settings->value("sub01_font_size").toInt();
    int weight = this->settings->value("sub01_font_weight").toInt();
    bool italic = this->settings->value("sub01_font_italic").toBool();
    QFont font(family, size, weight, italic);
    return font;
}

QColor Settings::getColorSubtitlePropertie01(){
    int red = this->settings->value("sub01_color_red").toInt();
    int green = this->settings->value("sub01_color_green").toInt();
    int blue = this->settings->value("sub01_color_blue").toInt();
    QColor color(red, green, blue);
    return color;
}

int Settings::getYSubtitlePropertie01(){
    return this->settings->value("sub01_y").toInt();
}

//03 : video
//04 : audio
void Settings::setVolume(int percentage){
    this->settings->setValue("sound", percentage);
}

int Settings::getVolume(){
    return this->settings->value("sound").toInt();
}



























